package com.java.androidmarket.service.impl;

import com.java.androidmarket.dao.CartDao;
import com.java.androidmarket.entity.Goods;
import com.java.androidmarket.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * description:
 * author:张腾
 * date:2021-04-30
 */

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartDao cartDao;

    @Override
    public List<Goods> getCart(int userId) {
        List<Goods> cartGoods = cartDao.getCart(userId);

        return cartGoods;
    }

    @Override
    @Transactional
    public void addCart(int userId, int goodsId) {

        if (cartDao.ifExist(userId,goodsId)!=0) {
            cartDao.incrNum(userId,goodsId);
        }else {
            cartDao.addCart(userId,goodsId);
            cartDao.incrNum(userId,goodsId);
        }
    }

    @Override
    public int updateGoodsNum(int userId, int goodsId, int goodsNum) {
        cartDao.updateGoodsNum(userId,goodsId,goodsNum);
        int updateGoodsNum = cartDao.selectNum(userId, goodsId);

        return updateGoodsNum;
    }

    @Override
    public void deleteGoods(int userId, int goodsId) {
        cartDao.deleteGoods(userId,goodsId);
    }
}
